package com.kbatis.handler;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.kbatis.annotation.Column;
import com.kbatis.config.MappedStatement;
import com.kbatis.utils.HighLightUtil;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.*;
import java.util.List;

/**
 * @ClassName ResultSetHandler
 * @Description
 * @Author 小黄
 * @Date 2019/11/16 15:56
 * @Version 1.0
 */
public class ResultSetHandler {
    private ResultSet resultSet;
    private MappedStatement mappedStatement;
    private List<Object> results;

    public ResultSetHandler(ResultSet resultSet, MappedStatement mappedStatement, List<Object> results) {
        this.resultSet = resultSet;
        this.mappedStatement = mappedStatement;
        this.results = results;
    }

    public void handleResultSet() throws Exception {
        Class<?> resultTypeClass = mappedStatement.getResultTypeClass();
        //取出resultTypeObject所有字段
        Field[] fields = resultTypeClass.getDeclaredFields();
        // 没遍历一次是一行数据，对应一个映射对象
        while (resultSet.next()) {
            Object result = resultTypeClass.newInstance();
            for (Field field : fields) {
                //暴力反射
                field.setAccessible(true);
                //获取字段column上对应的值
                String columnValue = getColumnValue(field);
                Object value = new Object();
                if (columnValue != null) {
                    String valueStr = resultSet.getString(columnValue);
                    if (StrUtil.isNotBlank(valueStr)) {
                        if (field.getType() == String.class) {
                            value = HighLightUtil.contentHighLight(valueStr, "###", "!!!");
                        } else if (field.getType() == java.util.Date.class) {
                            value = DateUtil.parseDate(valueStr);
                        } else if (field.getType() == Integer.class) {
                            value = Integer.parseInt(valueStr);
                        } else if (field.getType() == Timestamp.class) {
                            value = Timestamp.valueOf(valueStr);
                        } else if (field.getType() == Double.class) {
                            value = Double.parseDouble(valueStr);
                        } else if (field.getType() == BigDecimal.class) {
                            value = BigDecimal.valueOf(Long.parseLong(valueStr));
                        }
                    } else {
                        if (field.getType() == String.class) {
                            value = "";
                        } else if (field.getType() == Integer.class) {
                            value = Integer.valueOf(0);
                        } else if (field.getType() == Double.class) {
                            value = Double.valueOf(0);
                        } else if (field.getType() == BigDecimal.class) {
                            value = BigDecimal.ZERO;
                        } else {
                            value = null;
                        }
                    }
                    field.set(result, value);// 设置值
                }
            }
            results.add(result);
        }
    }

    private String getColumnValue(Field field) {
        Column annotation = field.getAnnotation(Column.class);
        if (null != annotation) {
            return annotation.value();
        }
        return null;
    }
}
